package com.ruanda.config;

import cn.hutool.core.util.ObjectUtil;
import com.ruanda.constant.AuthConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class FeignInterceptor implements RequestInterceptor {

    /**
     * feign拦截器
     * @param requestTemplate 即将要发起新的请求的请求对象
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 获取当前请求的上下文
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(ObjectUtil.isNotNull(requestAttributes)){
            // 获取当前请求对象
            HttpServletRequest request = requestAttributes.getRequest();
            if (ObjectUtil.isNotNull(request)){
                String token = request.getHeader(AuthConstants.AUTHORIZATION);
                requestTemplate.header(AuthConstants.AUTHORIZATION,token);
            }
        }
        requestTemplate.header(AuthConstants.AUTHORIZATION,AuthConstants.BEARER + "701440ac-27b9-4fc8-ad29-049ed6289cae");
    }
}